import {
  reqUserInfoList,
  reqUserCount,
  reqAdminList,
  reqAdminCount,
  reqOrderCount,
  reqOrderList,
  reqDetailOrderAdderss,
  reqDetailOrderUser,
  reqDetailOrderRes
} from '@/api/dataManage'

const state = {
  userInfo: [],
  userCount: '',
  adminList: [],
  adminCount: '',
  orderCount: '',
  orderList: [],
  detailOrder:{}
}

const mutations = {
  RECEIVED_USERINFOLIST(state, userInfo) {
    state.userInfo = userInfo
  },
  RECEIVED_USERCOUNT(state, userCount) {
    state.userCount = userCount
  },
  RECEIVED_ADMINLIST(state, adminList) {
    state.adminList = adminList
  },
  RECEIVED_ADMINCOUNT(state, adminCount) {
    state.adminCount = adminCount
  },
  RECEIVED_ORDERLIST(state, orderList) {
    state.orderList = orderList
  },
  RECEIVED_ORDERCOUNT(state, orderCount) {
    state.orderCount = orderCount
  },
  RECEIVED_DETAILORDER(state,detailOrder){
    state.detailOrder = detailOrder
  }
}

const actions = {
  //获取用户信息列表
  async getUserInfoListData({
    commit
  }, {
    limit,
    offset
  }) {
    const userInfoData = await reqUserInfoList(limit, offset)
    commit('RECEIVED_USERINFOLIST', userInfoData)
    return userInfoData
  },
  //获取用户数量
  async getUserCount({
    commit
  }) {
    const userCount = await reqUserCount()
    commit('RECEIVED_USERCOUNT', userCount.count)
    return userCount.count
  },

  //获取管理员列表
  async getAdminList({
    commit
  }, {
    limit,
    offset
  }) {
    const adminListData = await reqAdminList(limit, offset)
    if (adminListData.status === 1) {
      commit('RECEIVED_ADMINLIST', adminListData.data)
    }
  },

  //获取订单列表
  async getOrderList({
    commit
  }, {
    offset,
    limit
  }) {
    console.log(offset, limit)
    const orderListData = await reqOrderList(offset, limit)
    commit('RECEIVED_ORDERLIST', orderListData)
  },

  //获取管理员数量
  async getAdminCount({
    commit
  }) {
    const adminCountData = await reqAdminCount()
    if (adminCountData.status === 1) {
      commit('RECEIVED_ADMINCOUNT', adminCountData.count)
      return adminCountData.count

    }
  },


  //获取订单数量
  async getOrderCount({
    commit
  }) {
    const orderCountData = await reqOrderCount()
    commit('RECEIVED_ORDERCOUNT', orderCountData.count)
    return orderCountData.count
  },
  //获取订单的详细信息
  async getDetailOrder({
    commit
  }, {
    address_id,
    user_id,
    restaurant_id
  }) {
    console.log(address_id,
      user_id,
      restaurant_id)
    const adderssResult = await reqDetailOrderAdderss(address_id)
    const userResult = await reqDetailOrderUser(user_id)
    const resResult = await reqDetailOrderRes(restaurant_id)
    commit('RECEIVED_DETAILORDER', {
      adderssResult,
      userResult,
      resResult
    })
    // console.log(address_id,adderssResult)
    return {
      adderssResult,
      userResult,
      resResult
    }
  }
}

const getters = {}
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
